<template>
  <div class="scale-box" :id="dynamicIdName()">
    <div
      class="hall-task-box animate__animated"
      :class="dynamicClassName"
      style="width: 460px;height: 906px;"
      :id="dynamicChildName()"
    >
      <h1 class="h1_title">
        <span>办件分析</span>
      </h1>
      <div class="bj-wrap">
        <div class="bj-item">
          <p class="bj-name">受理量</p>
          <p class="num"><em>684</em>件</p>
        </div>
        <div class="bj-item">
          <p class="bj-name">受理量</p>
          <p class="num"><em>684</em>件</p>
        </div>
      </div>
      <h2 class="h2_title">
        <span>近半年受理趋势分析</span>
      </h2>
      <div class="chart-wrap">
        <LineChart></LineChart>
      </div>
      <h2 class="h2_title">
        <span>受理区域分布</span>
      </h2>
      <div class="chart-wrap">
        <BarChart></BarChart>
      </div>
      <h2 class="h2_title">
        <span>事项排名</span>
        <p class="h2_tab">
          <span :class="{ active: tabIndex == 0 }" @click="tabIndex = 0"
            >高频受理</span
          >
          <span :class="{ active: tabIndex == 1 }" @click="tabIndex = 1"
            >超期受理</span
          >
        </p>
      </h2>
      <BarHorizontal v-show="tabIndex == 0"> </BarHorizontal>

      <BarHorizontal
        v-show="tabIndex == 1"
        :barcolor="
          'linear-gradient(90deg, rgba(169, 71, 0, 0.30) -6.55%, #9F7B1D 88.1%)'
        "
      >
      </BarHorizontal>
    </div>
  </div>
</template>
<script>
import LineChart from "./LineChart.vue";
import BarChart from "./BarChart.vue";
import BarHorizontal from "./BarHorizontal.vue";
//引入封装方法
import encapsulation from "../../utils/aircimGird";
export default {
  //混入封装方法
  mixins: [encapsulation],
  components: { LineChart, BarChart, BarHorizontal },
  data() {
    return {
      tabIndex: 0,
      resizeObserver: null,
      //动态Id
      timestamp: null,
    };
  },
  mounted() {
    this.timestamp = new Date().getTime();
    this.$nextTick(() => {
      // 监听盒子变化
      this.listenChangeFn();
    });
  },
  methods: {
    //监听外部盒子的大小 然后对数据井行缩放
    scaleFn() {
      const parentBox = document.getElementById(`dynamic-${this.timestamp}`);
      const childBox = document.getElementById(
        `dynamicChild-${this.timestamp}`
      );
      const parentWidth = parentBox.offsetWidth;
      const parentHeight = parentBox.offsetHeight;
      const childWidth = childBox.offsetWidth;
      const childHeight = childBox.offsetHeight;
      let scaleX, scaleY;
      if (parentWidth < childWidth || parentHeight < childHeight) {
        const scaleBasedOnWidth = parentWidth / childWidth;
        const scaleBasedOnHeight = parentHeight / childHeight;
        scaleX = scaleBasedOnWidth;
        scaleY = scaleBasedOnHeight;
      } else {
        scaleX = parentWidth / childWidth;
        scaleY = parentHeight / childHeight;
      }
      childBox.style.transform = `scale(${scaleX}, ${scaleY})`;
    },
    // 监听盒子变化
    listenChangeFn() {
      let that = this;
      const element = document.getElementById(`dynamic-${this.timestamp}`);
      // console.log(element, "element");
      if (element) {
        new ResizeObserver(() => {
          // 处理元素大小变化的逻辑
          that.scaleFn();
        }).observe(element);
      }
    },
    // 绑定动画样式 animate__flash
    bindAnimationFn(animateName) {
      if (animateName != null) {
        this.dynamicClassName = animateName;
      }
    },
    //定义唯一ID名
    dynamicIdName() {
      return `dynamic-${this.timestamp}`;
    },
    //定义唯一ID名
    dynamicChildName() {
      return `dynamicChild-${this.timestamp}`;
    },
  },
  // 销毁监听
  beforeDestroy() {
    // 组件销毁前停止监听
    if (this.resizeObserver) {
      this.resizeObserver.disconnect();
    }
    if (this.requestTime != null) {
      clearInterval(this.requestTime);
    }
  },
};
</script>
<style lang="scss" scoped>
@import "../../static/assets/common.scss";
</style>
<style lang="scss" scoped>
.scale-box {
  width: 100%;
  height: 100%;
}

.hall-task-box {
  width: 460px;
  height: 906px;
  position: absolute;
  padding: 20px;
  box-sizing: border-box;
  overflow: hidden;
  background-image: url(../../../thumbnail/backImg/new-border-green.png);
  background-size: 100% 100%;
  background-repeat: no-repeat;
  transform-origin: left top;
  .grid-charts {
    width: 100%;
    height: 100%;
  }
}

.bj-wrap {
  margin-top: 20px;
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;

  .bj-item {
    position: relative;
    padding-left: 100px;
    width: 171px;
    height: 76px;
    display: inline-flex;
    flex-direction: column;
    justify-content: center;
    background: url(../../static/assets/HallTask/task-bg1.png) no-repeat left
      center;

    &::after {
      position: absolute;
      content: "";
      width: 35px;
      height: 39px;
      background-image: url(../../static/assets/HallTask/task-icon1.png);
      left: 23px;
      top: 17px;
      animation: transfromy 1s infinite alternate;
    }

    &:last-of-type::after {
      width: 41px;
      height: 39px;
      top: 18px;
      left: 23px;
      background-image: url(../../static/assets/HallTask/task-icon2.png);
    }

    p {
      justify-content: start;
    }
  }

  .bj-name {
    margin-bottom: 5px;
    line-height: 25px;
    font-size: 16px;
    color: #fff;
  }
}

.chart-wrap {
  margin-top: 10px;
  margin-bottom: 10px;
  position: relative;
  height: 177px;
}

@keyframes transfromy {
  0% {
    transform: translateY(0);
  }

  100% {
    transform: translateY(-3px);
  }
}
</style>
